package cn.kgc.util;

import cn.kgc.util.exception.HttpException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author wangyue
 * @version 1.0
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@SuppressWarnings("all")
public class Result<T> {
    /**
     * 状态码
     */
    private Integer code;
    /**
     * 返回数据
     */
    private T data;
    /**
     * 返回信息
     */
    private String message;

    public static Result<Object> success() {
        return Result.success(null);
    }
    public static Result<Object> success(Object data) {
        return Result.builder().code(ResultConstant.SUCCESS.getCode())
                .message(ResultConstant.SUCCESS.getMessage())
                .data(data)
                .build();
    }
    public static Result<Object>  error(HttpException httpException){
        return Result.builder().code(httpException.getCode())
                .message(httpException.getMessage())
                .build();
    }
    public static Result<Object> error(Exception exception){
        return error(exception,null);
    }
    public static Result<Object> error(Exception exception,Object data){
        return Result.builder().code(ResultConstant.ERROR.getCode())
                .message(ResultConstant.ERROR.getMessage())
                .data(data)
                .build();
    }
}
